<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.number" placeholder="区域编号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.name" placeholder="区域名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" @click="newFormVisible = true">新增</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>

    <!-- 操作栏位 -->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button type="success" @click="newFormVisible = true" size="small">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="outExcel" size="small">导出EXCEL</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 展示界面 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="Loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="affiliationName" fixed min-width="200"></el-table-column>
      <el-table-column align="center" label="区域编号" prop="affiliationNumber" fixed min-width="150"></el-table-column>
      <el-table-column align="center" label="地理位置" prop="site" min-width="150"></el-table-column>
      <el-table-column align="center" label="创建人员" prop="createman" min-width="150"></el-table-column>
      <el-table-column align="center" label="创建时间" prop="createtime" min-width="150"></el-table-column>
      <el-table-column align="center" label="备注" prop="remark" min-width="250"></el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            type="danger"
            size="small"
            @click="handleDel(scope.$affiliationName, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>
    <!-- 新增界面 -->
    <el-dialog title="新增" :visible.sync="newFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="newForm" :model="newArea" :rules="rule">
        <el-form-item label="区域名称" prop="affiliationName">
          <el-input v-model="newArea.affiliationName"></el-input>
        </el-form-item>
        <el-form-item label="地理位置" prop="site">
          <el-input v-model="newArea.site"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="newArea.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="newFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>
    <!-- 编辑界面 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="editForm" :model="alterArea" :rules="rule">
        <el-form-item label="区域名称" prop="affiliationName">
          <el-input v-model="alterArea.affiliationName"></el-input>
        </el-form-item>
        <el-form-item label="区域编号">
          <el-input v-model="alterArea.affiliationNumber" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="地理位置" prop="site">
          <el-input v-model="alterArea.site"></el-input>
        </el-form-item>
        <el-form-item label="创建人员">
          <el-input v-model="alterArea.createman" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input v-model="alterArea.createtime" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="alterArea.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit('editForm')" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { searchArea, alterArea, newArea, deleteArea } from "../../api/area.js";
export default {
  data() {
    return {
      filters: {
        number: "",
        name: "",
        page: 1
      },
      tableData: [
        {
          affiliationName: null,
          affiliationNumber: null,
          site: null,
          createman: null,
          createtime: null,
          remark: null
        }
      ],
      newArea: {
        affiliationName: null,
        site: null,
        createman: null,
        remark: null
      },
      alterArea: {
        affiliationName: null,
        affiliationNumber: null,
        site: null,
        createman: null,
        createtime: null,
        remark: null
      },
      rule: {
        affiliationName: [
          { required: true, message: "请输入区域", trigger: "blur" }
        ],
        site: [{ required: true, message: "请输入地理位置", trigger: "blur" }]
      },
      newFormVisible: false,
      alterFormVisible: false,
      Loading: false,
      currentPage: 1,
      totalPage: 1,
      editLoading: false,
      newLoading: false
    };
  },
  methods: {
    // 获取数据
    getData() {
      this.Loading = true;
      searchArea(this.filters).then(res => {
        this.Loading = false;
        if (res) {
          this.tableData = res.msg;
          this.totalPage = res.page;
        } else {
          this.tableData = [];
          this.totalPage = 0;
          this.currentPage = 0;
        }
      });
    },
    // 查询
    search() {
      this.filters.page = 1;
      this.getData();
    },
    // 分页
    handleCurrentChange(val) {
      this.filters.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    // 点击修改
    handleEdit(index, row) {
      this.alterFormVisible = true;
      this.alterArea = Object.assign({}, row);
    },
    // 确认修改
    editSubmit(forName) {
      this.$refs[forName].validate(val => {
        if (val) {
          this.editLoading = true;
          alterArea(this.alterArea).then(res => {
            this.editLoading = false;
            if (res) {
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: action => {
                  this.getData();
                  this.alterFormVisible = false;
                }
              });
            }
          });
        }
      });
    },
    // 新增
    newSubmit(forName) {
      this.$refs[forName].validate(val => {
        if (val) {
          this.newLoading = true;
          this.newArea.createman = JSON.parse(
            sessionStorage.getItem("user")
          ).user;
          newArea(this.newArea).then(res => {
            this.newLoading = false;
            if (res) {
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: action => {
                  this.getData();
                  this.newFormVisible = false;
                }
              });
            }
          });
        }
      });
    },
    selsChange: function(sels) {
      this.sels = sels;
    },
    handleDel(affiliationName, row) {
      // console.log(affiliationName, row.affiliationNumber);
      let affiliationNumber = row.affiliationNumber;
      // 提示是否删除
      this.$confirm("此操作将永久删除该区域下所有内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 进行删除操作
          this.Loading = true;
          deleteArea(affiliationNumber).then(res => {
            this.Loading = false;
            // this.getData();
            if (res) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // TODO: 导出
    outExcel() {
      window.location.href =
        "http://192.168.10.225:80/dormitoryA/building/Building/areaInfoExcelOut";
    }
  },
  mounted() {
    this.search();
  }
};
</script>
<style>
</style>